package com.lining.orangeservice.controller;

import com.lining.orangeservice.common.Result;
import com.lining.orangeservice.common.enums.PaymentMethod;
import com.lining.orangeservice.entity.abstractentity.Order;
import com.lining.orangeservice.entity.dto.request.OrderCreateRequest;
import com.lining.orangeservice.entity.dto.OrderQuery;
import com.lining.orangeservice.service.OrderService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 订单控制器
@RestController
@RequestMapping("/orders")
@Slf4j
public class OrderController {
    
    private final OrderService orderService;
    
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }
    
    @PostMapping
    public Result<Order> createOrder(@Valid @RequestBody OrderCreateRequest request) {
        Order order = orderService.createOrder(request);
        return Result.success(order);
    }
    
    @GetMapping("/{orderId}")
    public Result<Order> getOrder(@PathVariable Long orderId) {
        Order order = orderService.getOrder(orderId);
        return Result.success(order);
    }
    
    @PostMapping("/{orderId}/cancel")
    public Result<Void> cancelOrder(@PathVariable Long orderId) {
        orderService.cancelOrder(orderId);
        return Result.success();
    }
    
    @PostMapping("/{orderId}/pay")
    public Result<Void> payOrder(@PathVariable Long orderId, 
                               @RequestParam PaymentMethod method) {
        orderService.payOrder(orderId, method);
        return Result.success();
    }
    
    @GetMapping
    public Result<List<Order>> getUserOrders(OrderQuery query) {
        List<Order> orders = orderService.getUserOrders(query.getUserId(), query);
        return Result.success(orders);
    }
}